﻿using FlickrNet;
using soma.Helpers;
using soma.Models;
using soma.Models.Resources;
using System;
using System.Collections.Generic;
using System.Configuration;

namespace soma.Services
{
    public class FlickrSearch
    {
        private const string PhotoUrlFormat = "http://www.flickr.com/photos/{0}/{1}";

        private const string FlickrWebsite = "Flickr.com";
        private Flickr flickr;

        public FlickrSearch()
        {
            flickr = new Flickr(ConfigurationManager.AppSettings["FlickrAPIKey"], ConfigurationManager.AppSettings["FlickrSharedSecret"]);
        }

        public List<PhotoResource> SearchPhotosByTag(string tag, DateTime minUploadDate)
        {
            return SearchPhotosByTag(tag, Constants.DEFAULT_NO_RESOURCES_PHOTOS_PER_TAG, minUploadDate);
        }

        public List<PhotoResource> SearchPhotosByTag(string tag, int noEntries, DateTime minUploadDate)
        {
            List<PhotoResource> photoResourceList = new List<PhotoResource>();
            PhotoCollection photos = null;

            try
            {
                PhotoSearchOptions options = new PhotoSearchOptions();
                options.Tags = tag;
                // We get the double of what is wanted because a request is expensive so we need to have more data cached
                // so that future requests are faster
                options.PerPage = Math.Min(noEntries, Constants.MAXIMUM_NO_RESOURCES_PER_TAG) * 5;
                options.HasGeo = true;
                options.MinUploadDate = minUploadDate;
                options.Extras |= PhotoSearchExtras.DateUploaded | PhotoSearchExtras.Geo | PhotoSearchExtras.OriginalUrl
                    | PhotoSearchExtras.ThumbnailUrl | PhotoSearchExtras.Tags | PhotoSearchExtras.Media;
                photos = flickr.PhotosSearch(options);
                //      flickr.PhotosSearchAsync(options, Fl
                if (photos != null)
                {
                    foreach (Photo photo in photos)
                    {
                        if (photo.PhotoId != null && photo.UserId != null)
                        {
                            // Person person = flickr.PeopleGetInfo(photo.UserId);

                            PhotoResource photoResource = new PhotoResource(HashGenerator.GetHashString(String.Format(PhotoUrlFormat, photo.UserId, photo.PhotoId)), tag);
                            photoResource.Url = String.Format(PhotoUrlFormat, photo.UserId, photo.PhotoId);
                            photoResource.ThumbnailUrl = photo.SquareThumbnailUrl;
                            //photoResource.Author = person.RealName;
                            photoResource.Author = "Check website for more info";
                            photoResource.Website = FlickrWebsite;
                            photoResource.PhotoName = photo.Title;
                            photoResource.Date = photo.DateUploaded;
                            photoResource.DownloadedDate = DateTime.Now;

                            photoResource.ResourceCoordinates = new Coordinates(photo.Longitude, photo.Latitude);

                            photoResourceList.Add(photoResource);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
            }

            photoResourceList.Sort((x, y) => y.Date.CompareTo(x.Date));

            return photoResourceList;
        }
    }
}